function todoCtrl($scope, $location, $rootScope, $modal) {
    $scope.tx_area_org = 'write somethind...';
    $scope.tx_area = $scope.tx_area_org;

    $scope.btn_todo_org = "btn btn-primary";
    $scope.btn_done_org = "btn btn-white";

    $scope.btn_todo = $scope.btn_todo_org;
    $scope.btn_done = $scope.btn_done_org;

    $scope.todoListShow = true;
    $scope.doneListShow = false;

    $scope.getTodoList = function () {
        getTodoList($scope);
    }

    $scope.addTodo = function () {
        addTodo($scope);
    }

    $scope.finishTodo = function ($index, todoId) {
        finishTodo($scope, $index, todoId);
    }

    $scope.showAddTag = function () {
        showAddTag($scope);
    }

    $scope.changeBtn = function (btnType) {
        if("todo"===btnType){
            $scope.btn_todo =  $scope.btn_todo_org;
            $scope.btn_done =  $scope.btn_done_org;
            $scope.todoListShow = true;
            $scope.doneListShow = false;
        }else{
            $scope.btn_todo =  $scope.btn_done_org;
            $scope.btn_done =  $scope.btn_todo_org;
            $scope.todoListShow = false;
            $scope.doneListShow = true;
        }
    }
}

function getTodoList($scope) {
    $.ajax({
        url: '/todo/get_todo_list',
        success: function (data) {
            if (data.success) {
                $scope.$apply(function () {
                    $scope.todoList = data.todoList;
                    $scope.doneList = data.doneList;
                });
            }
        },
        error: function (error) {
            console.log(error);
        }
    });
}

function showAddTag($scope) {
    $scope.tx_area = $scope.tx_area_org;
    $scope.tag_add_show = !$scope.tag_add_show;
}

function finishTodo($scope, $index, todoId) {
    $.ajax({
        url: '/todo/finish_todo',
        data: {
            'todoId': todoId
        },
        success: function (data) {
            if (data.success) {
                $scope.$apply(function () {
                    var todo = $scope.todoList.splice($index, 1);
                    $scope.doneList.splice(0, 0,todo[$index])
                });
            }
        },
        error: function (error) {
            console.log(error);
        }
    });
}

function addTodo($scope) {
    $.ajax({
        url: '/todo/add_todo',
        data: {
            'content': $scope.tx_area
        },
        success: function (data) {
            if (data.success) {
                $scope.$apply(function () {
                    $scope.todoList.splice(0, 0, data.todo)
                    $scope.tag_add_show = false;
                });
            }
        },
        error: function (error) {
            console.log(error);
        }
    });
}


angular
    .module('inspinia')
    .controller('todoCtrl', todoCtrl)